﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;

namespace HamsterSimulator
{
    public class DelegateCommand : ICommand
    {
        protected Func<object, bool> canExecuteHandler;
        protected Action<object> executeHandler;

        public DelegateCommand(Action<object> ExecuteHandler, Func<object, bool> CanExecuteHandler = null)
        {
            if (ExecuteHandler == null)
                throw new ArgumentNullException("ExecuteHandler", "Command must have an ExecutionHandler");
            this.canExecuteHandler = CanExecuteHandler;
            this.executeHandler = ExecuteHandler;
        }

        public bool CanExecute(object parameter)
        {
            if (canExecuteHandler == null)
                return true;
            return canExecuteHandler(parameter);
        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public void Execute(object parameter)
        {
            executeHandler(parameter);
        }
    }
}
